<?
/*
-------------------------------
Script - EvaGb
-------------------------------
File - repalce_inc.php
-------------------------------
Function - Функции для замены в тексте
-------------------------------
*/
function mreplace($text) {
if ($GLOBALS['_gb_cnf']['wordwrap']) $text=wordwrap($text,$GLOBALS['_gb_cnf']['wordwrap'], "\r\n",1);
$text=htmlspecialchars($text,ENT_QUOTES);
$text = str_replace("\r","",$text);
$text = str_replace("\n","<br>",$text);
$text = str_replace("\\", "&#092;", $text);
return $text;
}
function latinconvert($text) {
   $text=strtolower($text);
   $r="а,б,в,г,д,е,ё,ж,з,и,й,к,л,м,н,о,п,р,с,т,у,ф,х,ц,ч,ш,щ,ъ,ы,ь,э,ю,я, ";
   $l="a,b,v,g,d,e,e,zh,z,i,i',k,l,m,n,o,p,r,s,t,u,f,h,c,ch,sh,sh',,ii,',e,u,ya,_";
   $r=explode(',',$r);
   $l=explode(',',$l);
   $text=preg_replace("#[^a-zA-Zа-яА-Я0-9\-_\.]#","",$text);
   $text=str_replace($r,$l,$text);
   return $text;
}

function bbcontrol($text,$thisbb) {
    for($i=0,$c=count($thisbb);$i<$c;$i++) {
        preg_match_all("#\[(".$thisbb[$i].")\]#isU",$text,$openbb);
        preg_match_all("#\[\/(".$thisbb[$i].")\]#isU",$text,$closebb);
        if (count($openbb[1])>count($closebb[1])) {
            if (preg_match("#\[\/".$thisbb[$i]."\]#is",$text)) {
                $exp=explode("[/".$thisbb[$i]."]",$text);
             for($z=0,$k=count($openbb[1])-count($closebb[1]);$z<$k;$z++) {
                $exp[0].="[/".$thisbb[$i]."]";
             }
             $text=join("[/".$thisbb[$i]."]",$exp);
           } else {
             $text=str_replace("[".$thisbb[$i]."]","&#091;".$thisbb[$i]."&#093",$text);
           }
        }else if (count($openbb[1])<count($closebb[1])) {
            if (preg_match("#\[".$thisbb[$i]."\]#is",$text)) {
                $exp=explode("[".$thisbb[$i]."]",$text);
                for($z=0,$k=count($closebb[1])-count($openbb[1]);$z<$k;$z++) {
                    $exp[0].="[".$thisbb[$i]."]";
                }
                $text=join("[".$thisbb[$i]."]",$exp);
            }else {
                $text=str_replace("[/".$thisbb[$i]."]","&#091;/".$thisbb[$i]."&#093",$text);
            }
        }
    }
    return $text;
}

function bbreplace($text) {
$text=bbcontrol($text,array('quote'));
$who=array("#\[quote\]#is",
           "#\[\/quote\]#is",
           "#\[b\](.*)\[\/b\]#iUs",
           "#\[i\](.*)\[\/i\]#iUs",
           "#\[s\](.*)\[\/s\]#iUs",
           "#\[u\](.*)\[\/u\]#iUs",
           "#\[url=(.+?)\](.+?)\[\/url\]#is",
           "#\[img=(.+?) align=(.*?)\]#is",
           "#\[img=(.+?)\]#is",
           "#\[color=(.+?)\](.+?)\[\/color\]#is",
           "#\[fontstyle=(.+?)\](.+?)\[\/fontstyle\]#is",
           "#\[size=(.+?)\](.+?)\[\/size\]#is",
           "#\[center\](.+)\[\/center\]#iUs",
           "#\[left\](.+)\[\/left\]#iUs",
           "#\[right\](.+)\[\/right\]#iUs",
           );
$rewho=array(
             "<!--start_quote--><div class=\"quote\"><span style='color:#000000;font-style:normal;'><b><u>Цитата:</u></b></span><br><!--quote-->",
             "<!--quote--></div><!--end_quote-->",
             "<b>\\1</b>",
             "<i>\\1</i>",
             "<s>\\1</s>",
             "<u>\\1</u>",
             "<!--url--><a href=\"\\1\" target=_blank>\\2</a><!--/url-->",
             "<!--img--><img src=\"\\1\" align='\\2'><!--/img-->",
             "<!--img--><img src=\"\\1\"><!--/img-->",
             "<!--color--><span style=\"color:\\1\">\\2</span><!--/color-->",
             "<!--fontstyle--><span style=\"font-family:\\1\">\\2</span><!--/fontstyle-->",
             "<!--size--><span style=\"font-size:\\1\">\\2</span><!--/size-->",
             "<center>\\1</center>",
             "<!--left--><div align=\"left\">\\2</div><!--/left-->",
             "<!--right--><div align=\"right\">\\2</div><!--/right-->",
             );
return preg_replace($who,$rewho,$text);
}
function replace($text) {
$text=mreplace($text);
$text=bbreplace($text);
$handle =opendir($GLOBALS['_gb_cnf']['dir'].'/sis/smile');
    while($file = readdir ($handle)) {
	if ($file == "." || $file == ".."){
              continue;
                }
              if (is_img($file)) {
                $text=str_replace(":$file:","<img src=\"{$GLOBALS['_gb_cnf']['dir']}/sis/smile/$file\" >",$text);
              }
         }
closedir($handle);
return $text;
}

function r_bbreplace($text) {
    $rewho=array(
           '[quote]',
           '[/quote]',
           '[b]\\1[/b]',
           '[i]\\1[/i]',
           '[s]\\1[/s]',
           '[u]\\1[/u]',
           '[url=\\1]\\2[/url]',
           '[img=\\1 align=\\2]',
           '[img=\\1]',
           "[color=\\1]\\2[/color]",
           "[fontstyle=\\1]\\2[/fontstyle]",
           "[size=\\1]\\2[/size]",
           "[center]\\1[/center]",
           "[left]\\1[/left]",
           "[right]\\1[/right]",
           );
$who=array(
             "#<!--start_quote-->(.+?)<!--quote-->#is",
             "#<!--quote--><\/div><!--end_quote-->#is",
             "#<b>(.*)<\/b>#iUs",
             "#<i>(.*)<\/i>#iUs",
             "#<s>(.*)<\/s>#iUs",
             "#<u>(.*)<\/u>#iUs",
             "#<!--url--><a href=\"(.+)\" target=_blank>(.+)<\/a><!--\/url-->#iUs",
             "#<!--img--><img src=\"(.+)\" align='(.*)'><!--\/img-->#iUs",
             "#<!--img--><img src=\"(.+)\"><!--\/img-->#iUs",
             "#<!--color--><span style=\"color:(.+)\">(.+)<\/span><!--\/color-->#iUs",
             "#<!--fontstyle--><span style=\"font-family:(.+)\">(.+)<\/span><!--\/fontstyle-->#iUs",
             "#<!--size--><span style=\"font-size:(.+)\">(.+)<\/span><!--\/size-->#iUs",
             "#<center>(.+)<\/center>#iUs",
             "#<!--left--><div align=\"left\">(.+)<\/div><!--\/left-->#iUs",
             "#<!--right--><div align=\"right\">(.+)<\/div><!--\/right-->#iUs",
             );
$text=preg_replace($who,$rewho,$text);
return $text;
}
function r_replace($text) {
$text=str_replace('<br>',"\r\n",$text);
$text=r_bbreplace($text);
$text = str_replace("javascript<b></b>:" ,"javascript:" , $text );
$text = str_replace("vdscript<b></b>:" ,"vdscript:" , $text );
$handle =opendir($GLOBALS['_gb_cnf']['dir'].'/sis/smile');
    while($file = readdir ($handle)) {
	if ($file == "." || $file == ".."){
              continue;
                }
              if (is_img($file)) {
                $text=str_replace("<img src=\"{$GLOBALS['_gb_cnf']['dir']}/sis/smile/$file\" >",":$file:",$text);
            }
         }
closedir($handle);
$text=htmlspecialchars(html_entity_decode($text,ENT_QUOTES));
 return $text;
}
?>
